<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表页</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/列表页面专用CSS.css">
</head>
<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="image/狗狗.png" alt="">
        <sapn class="title">我的博客系统</sapn>
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout">注销</a>
    </div>
    
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧区域，显示用户信息 -->
        <div class="container-left">
            <!-- 用户详情 -->
            <div class="card">
                <!-- 头像 -->
                <img src="image/头像.png" alt="">
                <!-- 用户名字 -->
                <h3></h3>
                <!-- gitee地址链接 -->
                <a href="https://gitee.com/Coach_zhang">博主的gitee链接</a>
                <!-- 显示文章分类的数目 -->
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右侧区域，显示博客列表 -->
        <div class="container-right">
            <!-- <div class="blog">
                <div class="title">第一篇博客</div>
                <div class="date">发布日期：2022-02-26</div>
                <div class="desc">
                    Lorem ipsum dolor sit amet consectetur adipisicing elit. Sequi corrupti a magnam velit autem impedit expedita recusandae eum asperiores voluptate blanditiis, ullam deleniti, quidem rerum. Nam facilis debitis ducimus a?
                </div>
                <a href="#" class="detail">查看全文 &gt; &gt;</a>
            </div> -->
        </div>
    </div>

    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js">
    </script>

    <script>
        $.ajax({
            url:"blog",
            method:'get',
            success:function(data,status){
                buildBlogs(data)
            },

            error:function(){
                location.assign('blog_login.html');
            }
        });

        function buildBlogs(data){

            let container=document.querySelector('.container-right');

            for(let blog of data){
                let blogDiv=document.createElement('div');
                blogDiv.className='blog';

                let titleDiv=document.createElement('div');
                titleDiv.className='title';
                titleDiv.innerHTML=blog.title;
                blogDiv.appendChild(titleDiv);

                let dateDiv=document.createElement('div');
                dateDiv.className='date';
                dateDiv.innerHTML=formatDate(blog.postTime);
                blogDiv.appendChild(dateDiv);

                let descDiv=document.createElement('div');
                descDiv.className='desc';
                descDiv.innerHTML=blog.content;
                blogDiv.appendChild(descDiv);

                let detailA=document.createElement('a');
                detailA.className='detail';
                detailA.innerHTML='查看全文 &gt; &gt;';
                detailA.href='blog_detail.html?blogId='+ blog.blogId;
                blogDiv.appendChild(detailA);

                container.appendChild(blogDiv);
            }
        }
        
        function formatDate(time){
            var date = new Date(time);

	        var year = date.getFullYear(),
		        month = date.getMonth()+1,
		        day = date.getDate(),
		        hour = date.getHours(),
		        min = date.getMinutes(),
		        sec = date.getSeconds();
	        var newTime = year + '-' +
				(month < 10? '0' + month : month) + '-' +
				(day < 10? '0' + day : day) + ' ' +
				(hour < 10? '0' + hour : hour) + ':' +
				(min < 10? '0' + min : min) + ':' +
				(sec < 10? '0' + sec : sec);
	        return newTime;	
        }

        $.ajax({
           url:'user',
           method:'get',
           success:function(data,status){
               changeUser(data);
           } 
        });

        function changeUser(user){
            let h3=document.querySelector('.container-left .card h3');
            h3.innerHTML=user.username;
        }
    </script>
</body>
</html>